<!--suppress ALL -->
<template>
	<el-dialog title=" 文件上传" :close-on-click-modal="false" :visible.sync="visible">
		<el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="130px">

			<el-form-item label="名单文件" prop="fileList">
				<el-upload class="upload-demo" action="as" :limit="1" accept=".xlsx,.xls" :http-request="taskUploadHttpRequest" :file-list="taskUploadFileList" :auto-upload="true" :show-file-fist="false" multiple>
					<i class="el-icon-upload" title="发送图片">文件上传</i>
					<div slot="tip" class="el-upload__tip">
						支持扩展名:xlsx的excel文件
						<el-button style="margin-left: 20px;" @click="downloadFile()" size="small" type="primary">模板下载</el-button>
					</div>
				</el-upload>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer"><el-button @click="dataFormSubmit()">保存</el-button>
    </span>
	</el-dialog>
</template>

<script>
	export default {
		data() {
			return {
				dataForm: {},
				visible: false,
				ignoreOne: false,
				taskUploadFileList: [],
				ftpPath: '',
				analysisStatus: '',
			}
		},
		created() {},
		methods: {
			init: function() {
				this.taskUploadFileList = []
				var that = this
				this.visible = true
			},
			downloadFile: function() {
				var that = this
				var httpUrl = process.env.FTP_URL
				btwApiUtil.postDownLoadFile('cc.customer.label.model.download', {}, function(resp) {}, "客户标签模板.xlsx");
			},
			dataFormSubmit() {
				this.$refs['dataForm'].validate((valid) => {
					var that = this
					if(that.ftpPath == '') {
						that.$message.error('请提交excel文件')
						return
					}
					btwApiUtil.post('cc.customer.label.model.upload', {
						ftpUrl: this.ftpPath
					}, function(resp) {
						if(resp.data.code == '0') {
							that.$message({
								message: '上传成功,后台正在解析中,请稍后刷新',
								type: 'success',
								duration: 1500,
								onClose: () => {
									that.visible = false;
									that.$emit('refreshDataList')
								}
							});
						} else {
							that.ftpPath == ''
							that.$message.error(resp.data.msg)
						}
					})

				})
			},
			taskUploadHttpRequest(fe) {
				var file = fe.file
				var that = this
				let formData = new FormData()
				formData.append('file', file)
				btwApiUtil.post('common.uploadfile.single', {}, function(resp) {
					that.taskUploadFileList = []
					//ftp地址
					that.ftpPath = resp.data.ftpPath
				}, formData)
			}
		}
	}
</script>